package vn.sms.vsms;

 
import java.util.ArrayList;

import vn.sms.vsms.data.ServiceItem;
import vn.sms.vsms.provider.DbApdater;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;

public class ListActivity extends BaseActivity {
	ExpandableListView listview ;
	
	private ArrayList<Integer> groupExpanded = new ArrayList<Integer>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		Intent intent = getIntent();
		int id = intent.getIntExtra(Contants.SMS_EXTRA_ID,0);
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		Log.i("logging","id" + id);
		final ServiceItem currentItem =  DbApdater.getItemChildsByid(id);
		
		TextView titleBar = (TextView)findViewById(R.id.str_service);
		titleBar.setText(currentItem.getName());
		
		Log.i("logging",String.valueOf(currentItem.getName()));
		listview = (ExpandableListView)findViewById(R.id.list);		
		ListMenuAdapter adapter 	= new ListMenuAdapter(this,currentItem);
		listview.setAdapter(adapter);
		listview.setOnGroupClickListener(new OnGroupClickListener() {
			
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				 Contants.onItemClick(v.getContext(), currentItem.getChilds().get(groupPosition));
				return false;
			}
		});
		
		listview.setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				try{
					ServiceItem item = currentItem.getChilds().get(groupPosition).getChilds().get(childPosition);
					Contants.onItemClick(v.getContext(), item);
				}catch(Exception e){
					
				}
				
				return false;
			}
		});
		
		listview.setOnGroupExpandListener(new OnGroupExpandListener() {
			
			@Override
			public void onGroupExpand(int groupPosition) {
				groupExpanded.add(groupPosition);				 				
			}
		});
		
		listview.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			
			@Override
			public void onGroupCollapse(int groupPosition) {
				try{
					int index = groupExpanded.indexOf(groupPosition);
					groupExpanded.remove(index);
				}catch(Exception e){
					
				}
				
				 
			}
		});
	}
	
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
	    super.onWindowFocusChanged(hasFocus);
	    listview.setIndicatorBounds(listview.getRight()- 40, listview.getWidth());
	} 
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}


	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if(groupExpanded != null && groupExpanded.size() > 0 )
		{
			try{
				int size = groupExpanded.size();
				listview.collapseGroup(groupExpanded.get(size-1));
			}catch(Exception e){
				groupExpanded.clear();				
			}
			
		}else
		{
			super.onBackPressed();
		}
		
	}
	
	
	

	

}
